package com.lzh.persevere.utils;

import cn.hutool.core.util.StrUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 字符串相关工具类
 */
public class StringUtils {

    /**
     * 将字符串保留到指定长度，若字符串长度不够，则在左侧补0
     *
     * @param originStr 原字符串
     * @param targetLen 保留长度
     * @return 字符串
     */
    public static String keepTargetDigitsByZeroLeft(String originStr, int targetLen) {
        if (originStr.length() >= targetLen) {
            return originStr;
        }
        StringBuilder sb = new StringBuilder(originStr);
        int temp = targetLen - sb.length();
        if (temp > 0) {
            while (sb.length() < targetLen) {
                //若长度不足进行补零, 每次都在最前面补零
                sb.insert(0, "0");
            }
        }
        return sb.toString();
    }

    /**
     * 将字符串保留到指定长度（按照最长的字符串补齐位数），若字符串长度不够，则在左侧补0
     *
     * @param str1 字符串1
     * @param str2 字符串2
     * @return 字符串数组
     */
    public static String[] keepTwoStrSameLen(String str1, String str2) {
        String[] arr = {"", ""};
        if (StrUtil.isEmpty(str1) || StrUtil.isEmpty(str2)) {
            return arr;
        }
        int maxLen = Math.max(str1.length(), str2.length());
        String s1 = keepTargetDigitsByZeroLeft(str1, maxLen);
        String s2 = keepTargetDigitsByZeroLeft(str2, maxLen);
        arr[0] = s1;
        arr[1] = s2;
        return arr;
    }

    /**
     * 字符串相减
     *
     * @param str1 字符串1
     * @param str2 字符串2
     * @return 字符串相减差值（正数）
     */
    public static int sub(String str1, String str2) {
        if (str1.length() != str2.length()) {
            return -1;
        }
        // str1为较大值，str2为较小值
        if (str1.compareTo(str2) <= 0) {
            String temp = str1;
            str1 = str2;
            str2 = temp;
        }

        char[] charArray1 = str1.toCharArray();
        char[] charArray2 = str2.toCharArray();

        int[] diff = new int[str1.length()];
        int carry = 0;

        for (int i = charArray2.length - 1; i >= 0; i--) {
            int val1 = charArray1[i] - '0';
            int val2 = charArray2[i] - '0';
            int sum = val1 - val2 - carry;

            if (sum < 0) {
                sum += 10;
                carry = 1;
            } else {
                carry = 0;
            }

            diff[i] = sum;
        }

        for (int i = charArray2.length; i < charArray1.length; i++) {
            int val1 = charArray1[i] - '0';
            int sum = val1 - carry;

            if (sum < 0) {
                sum += 10;
                carry = 1;
            } else {
                carry = 0;
            }

            diff[i] = sum;
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < diff.length; i++) {
            if (i == 0 && diff[i] == 0) {
                continue; // Skip leading zeros
            }
            sb.append(diff[i]);
        }

        String str = sb.toString();
        return Integer.parseInt(str);
    }

    /**
     * 移除小数点
     *
     * @param str 字符串
     * @return 结果
     */
    public static String removeDecimal(String str) {
        if (StrUtil.isEmpty(str)) {
            return str;
        }
        if (str.contains(".")) {
            return str.substring(0, str.indexOf("."));
        }
        return str;
    }

    /**
     * 移除小数点
     *
     * @param str 字符串
     * @return 结果
     */
    public static String mergeArr(String[] str, int n) {
        if (str == null || str.length == 0) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(str[i]).append("\r\n");
        }
        return sb.toString();
    }

    /**
     * 移除小数点
     *
     * @param str 字符串
     * @return 结果
     */
    public static String mergeArr(List<String> str, int n) {
        if (str == null || str.size() == 0) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(str.get(i)).append("\r\n");
        }
        return sb.toString();
    }

    public static String getStrByIndexRange(String[] arr, int begin, int end) {
        int arrLen = arr.length;
        if (arrLen < begin + 1) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = begin; i < arrLen && i < end; i++) {
            sb.append(arr[i]).append("\r\n");
        }
        return sb.toString();
    }

    public static String getStrByIndexRange(List<String> arr, int begin, int end) {
        int arrLen = arr.size();
        if (arrLen < begin + 1) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = begin; i < arrLen && i < end; i++) {
            sb.append(arr.get(i)).append("\r\n");
        }
        return sb.toString();
    }

    public static String getStrByIndexRange(List<String> list) {
        return list.stream().collect(Collectors.joining("\r\n"));
    }
}
